package com.Drools.敲定的几种模式;



import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class 读指定文件drl {
    public static void main(String[] args) {

        KieHelper kieHelper = new KieHelper();
        String drl = readerFile("D:\\Work\\IDEA\\Drools\\src\\main\\resources\\oldRules\\rules-x.drl");
        kieHelper.addContent(drl,"test.drl");
        KieBase build = kieHelper.build();
        KieSession kieSession = build.newKieSession();
        Map<String,Object> map1 = new HashMap<>();
        Map<String,Object> map2 = new HashMap<>();
        map1.put("a",1);
        map1.put("b",2);
        map1.put("m",map2);
        map2.put("a","ss");
        map2.put("b",map2);
        kieSession.insert(map1);
        kieSession.fireAllRules();
        System.out.println(map1);
        kieSession.dispose();
    }

    public static String readerFile(String path){
        StringBuilder sb = new StringBuilder();
        try(BufferedReader r = new BufferedReader(new FileReader(path))){
            char[] c = new char[2048];
            int len;
            while ((len = r.read(c)) !=-1){
                sb.append(c,0,len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return sb.toString();
    }
}
